package com.hzkq.widgets.chart.simple;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.widget.Button;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.radar.XAxis;
import com.keqiang.chart.impl.radar.YAxis;
import com.keqiang.chart.impl.radar.impl.RadarChart;
import com.keqiang.chart.impl.radar.impl.RadarChartHighLightRender;
import com.keqiang.chart.impl.radar.impl.RadarChartView;
import com.keqiang.chart.impl.radar.impl.RadarData;
import com.keqiang.chart.impl.radar.impl.RadarEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * @author Created by 汪高皖 on 2020/1/4 10:26
 */
public class RadarChartActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private RadarChart mRadarChart;
    private Button mBtRefresh;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_radar_chart;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        RadarChartView radarChartView = findViewById(R.id.radar_chart);
        mRadarChart = radarChartView.getChart();
        mBtRefresh = findViewById(R.id.bt_refresh);
        
        initLineChart();
    }
    
    @Override
    public void initData() {
        fillLineChart1Data();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
        
        mBtRefresh.setOnClickListener(v -> fillLineChart1Data());
    }
    
    private void initLineChart() {
        mRadarChart.setRadius(SimpleUtil.getScaledValue(350));
        mRadarChart.setDrawStartAngle(30f);
        
        XAxis xAxis = mRadarChart.getXAxis();
        // xAxis.setLineStartOffset(SimpleUtil.getScaledValue(66));
        // xAxis.setLineEndPadding(SimpleUtil.getScaledValue(20));
        xAxis.setLineWidth(SimpleUtil.getScaledValue(2));
        xAxis.setLabelCount(6);
        xAxis.setLabelTextSize(SimpleUtil.getScaledValue(26));
        xAxis.setAngleErrorRange(10f);
        xAxis.setAxisValueFormat(pos -> {
            return "测试数据" + pos;
        });
        
        DashPathEffect pathEffect = new DashPathEffect(new float[]{10f, 10f}, 0);
        YAxis yAxis = mRadarChart.getYAxis();
        yAxis.setLineWidth(SimpleUtil.getScaledValue(2));
        // yAxis.setLineStyle(LineStyle.CIRCLE);
        yAxis.setLabelDrawStartAngle(-30f);
        yAxis.setDrawLabel(true);
        yAxis.setLinePathEffectFactory(pos -> {
            if (pos == 3) {
                return pathEffect;
            }
            
            return null;
        });
        
        // yAxis.setLineDrawFactory(pos -> {
        //     return pos >= 2;
        // });
        
        mRadarChart.setHighLightRender(new RadarChartHighLightRender(mRadarChart, 0x33000000, 2f));
    }
    
    /**
     * 设置图表数据
     */
    private void fillLineChart1Data() {
        List<RadarData> data = new ArrayList<>();
        XAxis xAxis = mRadarChart.getXAxis();
        YAxis yAxis = mRadarChart.getYAxis();
        
        for (int j = 0; j < 2; j++) {
            List<RadarEntity> entityList = new ArrayList<>();
            int count = xAxis.getLabelCount();
            for (int i = 0; i < count; i++) {
                RadarEntity entity =
                    new RadarEntity(i, (float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
                entity.setDrawLabel(false);
                entity.setLabel(((int) entity.getValue()) + "%");
                
                entity.setLabelOffset(10);
                if (j == 0) {
                    entity.setIndicatorDrawable(ContextCompat.getDrawable(this, R.drawable.bg_circle_blue));
                } else {
                    entity.setIndicatorDrawable(ContextCompat.getDrawable(this, R.drawable.bg_circle_red));
                }
                entity.setDrawIndicator(true);
                
                entityList.add(entity);
            }
            
            RadarData radarData = new RadarData(entityList);
            radarData.setLineWidth(SimpleUtil.getScaledValue(4));
            radarData.setFillBg(true);
            if (j == 0) {
                radarData.setFillBgColor(0x335283C9);
            } else {
                radarData.setFillBgColor(0x33BE3E38);
            }
            radarData.setLineColor(j == 0 ? 0xFF106CF5 : Color.RED);
            data.add(radarData);
        }
        
        mRadarChart.setData(data);
        mRadarChart.animDraw(2000);
    }
}
